package edu.tjut.tjutcommunity.config;

import edu.tjut.tjutcommunity.Interceptor.RootRequestInterceptor;
import edu.tjut.tjutcommunity.Interceptor.UserRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan(basePackages = "edu.tjut.tjutcommunity") //全局异常处理类需要被扫描才能
public class WebMvcConfig implements WebMvcConfigurer {


    @Bean
    UserRequestInterceptor getUserRequestInterceptor(){
        return new UserRequestInterceptor();
    }
    @Bean
    RootRequestInterceptor getRootRequestInterceptor(){
        return new RootRequestInterceptor();
    }
    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //token 身份认证管理
        registry.addInterceptor(getUserRequestInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login",
                        "/post/post",
                        "/floor/floor",
                        "/comment/comment",
                        "/",
                        "/hello",
                        "/comment/allComment/*",
                        "/image/*",
                        "/post/*",
                        "/user/checkUser");
        //权限管理拦截器
        registry.addInterceptor(getRootRequestInterceptor())
                .addPathPatterns("/post/del/*",
                        "/floor/del/*");
    }

}